import {
  BadRequestException,
  Controller,
  Post,
  Req,
  UploadedFile,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common';
import { ApiBody, ApiConsumes, ApiProperty, ApiTags } from '@nestjs/swagger';
import { UploadService } from '@/modules/upload/upload.service';
import { RolesGuard } from '@/auth/guard/roles.guard';
import { CurrentUser } from '@/decorators/current-user.decorator';
import { UserUploadEntity } from '@/entity/user-upload.entity';
import { FileInterceptor } from '@nestjs/platform-express';
import * as Multer from 'multer';
import { JwtAuthGuard } from '@/auth/guard/jwt-auth.guard';
import { diskStorage } from 'multer';
@Controller('upload')
@ApiTags('上传模块')
@UseGuards(JwtAuthGuard, RolesGuard)
export class UploadController {
  constructor(private readonly uploadService: UploadService) {}

  @Post('image')
  @UseInterceptors(FileInterceptor('file'))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: '上传图片文件',
    type: 'multipart/form-data',
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
        },
      },
    },
  })
  async uploadImage(
    @UploadedFile() file: Multer.File,
    @CurrentUser() currentUser,
    @Req() request: any,
  ): Promise<any> {
    if (!file) throw new BadRequestException('请上传图片文件');
    // 判断文件类型是否是图片
    const allowedTypes = [
      'image/jpg',
      'image/jpeg',
      'image/png',
      'image/gif',
      'image/webp',
      'image/svg+xml',
      'image/bmp',
      'image/x-icon',
      'image/tiff',
    ];
    if (!allowedTypes.includes(file.mimetype)) {
      throw new BadRequestException('只允许上传图片文件');
    }
    // 判断文件大小是否小于20mb
    const maxSize = 20 * 1024 * 1024; // 20mb
    if (file.size > maxSize) {
      throw new BadRequestException('上传文件大小不能超过20mb');
    }
    const { ip } = request; //获取ip
    const user_id = currentUser.user_id; //获取用户id
    const url = await this.uploadService.uploadToQiniu(file, 'image'); //上传到七牛云
    const upload = await this.uploadService.uploadImage(
      user_id,
      request.ip,
      url,
    ); //保存到数据库
    return upload;
  }

  @Post('video')
  @UseInterceptors(FileInterceptor('file'))
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: '上传视频文件',
    type: 'multipart/form-data',
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
        },
      },
    },
  })
  async uploadVideo(
    @UploadedFile() file: Multer.File,
    @CurrentUser() currentUser,
    @Req() request: any,
  ): Promise<any> {
    if (!file) throw new BadRequestException('请上传视频文件');
    // 判断文件类型是否是视频
    const allowedTypes = [
      'video/mp4',
      'video/odd',
      // 其他视频类型
    ];
    if (!allowedTypes.includes(file.mimetype)) {
      throw new BadRequestException('只允许上传mp4/odd类型的视频文件');
    }
    // 判断文件大小是否小于200mb
    const maxSize = 200 * 1024 * 1024; // 200mb
    if (file.size > maxSize) {
      throw new BadRequestException('上传文件大小不能超过200mb');
    }
    const { ip } = request; //获取ip
    const user_id = currentUser.user_id; //获取用户id
    const url = await this.uploadService.uploadToQiniu(file, 'video'); //上传到七牛云
    if (!url) throw new BadRequestException('上传失败');
    const upload = await this.uploadService.uploadVideo(
      user_id,
      request.ip,
      url,
    ); //保存到数据库
    return upload;
  }
}
